<?php
namespace app\common\model;

class ShopShip extends Common
{

    protected $autoWriteTimestamp = true;
    protected $createTime = 'ctime';
    // 追加属性
    protected $append = [
        'ctime_text'
    ];

    //获取器
    public function getCtimeTextAttr($value,$data){
        $value = $value ? $value : (isset($data['ctime']) ? $data['ctime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }
    /**
     * 店铺表关联
     * @return [type] [description]
     */
    public function shop(){
        return $this->hasOne(Shop::class,'id','shop_id');
    }
    /**
     * 删除分类
     */
    public function delShip($params){
        if(!empty($params['id'])){
            $ship=$this->where('id',$params['id'])->find();
            if(!$ship){
                return [false,'地址已经删除'];
            }
            $res=$ship->delete();
            if(!$res){
                return [false,'地址删除失败'];
            }
            return [true,'地址删除成功'];
        }
        return [true,'地址删除失败'];
    }
    /**
     * 更新或修改分类
     */
    public function updateShip($params){
        $character = ["\r\n", "\n", "\r"];
        $params['address'] = str_replace($character, '', $params['address']);
        if(empty($params['id'])){
            $maps=['is_def'=>1];
        }else{
            $maps=[
                ['is_def','=',1],
                ['id','<>',$params['id']]
            ];
        }
        $ship=$this->where($maps)->find();
        if(empty($params['id'])){
            $result=$this->create($params,true);
            if(!$result){
                return [false,'保存失败'];
            }
            if($ship && $params['is_def']==1){
                $this->where('id','<>',$result->id)->update(['is_def'=>2]);
            }
        }else{
            $result=$this->save($params,['id'=>$params['id']]);
            if(!$result){
                return [false,'保存失败'];
            }
            if($ship && $params['is_def']==1){
                $this->where('id','<>',$params['id'])->update(['is_def'=>2]);
            }
        }
        return [true,'保存成功'];
    }
    /**
     * 获取地址
     */
    public function getList($params){
        $list=$this->where('shop_id',$params['shop_id']);
        $total=$list->count();
        $result=$list->page($params['page'],$params['limit'])->order('is_def desc')->select();
        if($result && count($result) > 0){
            foreach($result as $k => &$v)
            {
                $v['area_name'] = get_area($v['area_id']);
            }
        }
        return [true,['list'=>$result,'total'=>$total]];
    }

    protected function tableWhere($post)
    {
        $where = [];
        if (isset($post['start']) && $post['start'] != "") {
            $where[] = ['ctime', '>=', strtotime($post['start'])];
        }
        if (isset($post['end']) && $post['end'] != "") {
            $where[] = ['ctime', '<', strtotime($post['end'])];
        }
        if (isset($post['search']) && $post['search'] != "") {
            $where[] = ['name', 'like', '%' . $post['search'] . '%'];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = "id desc";
        return $result;
    }
    /**
     * 返回layui的table所需要的格式
     * @author sin
     * @param $post
     * @return mixed
     */
    public function tableData($post, $isPage = true)
    {
        if(isset($post['page'])){
            $page=$post['page'];
        }else{
            $page=1;
        }
        if (isset($post['limit'])) {
            $limit = $post['limit'];
        } else {
            $limit = config('paginate.list_rows');
        }
        $tableWhere = $this->tableWhere($post);
        $list    = $this->field($tableWhere['field'])
                ->where($tableWhere['where'])
                ->order($tableWhere['order'])
                ->page($page,$limit)
                ->select();
        foreach ($list as $k=>$v){
            $list[$k]['area_name']=get_area($v['area_id']);
        }
        $re['count'] = count($list);
        $re['code'] = 0;
        $re['msg']  = 'success';
        $re['data'] = $list->toArray();
        return $re;
    }

}